// pages/user/login/login.js
var mta = require('../../../utils/mta_analysis.js');
var app = getApp();
var countdown = 120;
var reget = '获取验证码';
var timer;
var isTimer = false;//timer是否开始
// 定时器
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true,
    })
    countdown = 120;
    reget = '重新获取';
    isTimer = false;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })
    isTimer = true;
    countdown--;
    reget = '重新获取';
  }
  timer = setTimeout(function () {
    settime(that)
  }, 1000)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginBg: app.globalData._network_path + 'login_bg.png',
    userInfo: '', //获取到的用户信息

    type: 1, //默认1,1账号登录,2微信登录,3手机登录,4注册

    account: '',//账号
    password: '',//密码

    phone: '',//手机号码
    simcode: '',//验证码
    getOnecode: reget,//按钮内容
    last_time: '',//剩余时间
    is_show: true,//是否显示倒计时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    mta.Page.init();
  },
  onShow() {
    wx.setStorageSync('is_login', false);
    app.wxLogin();
  },

  //输入框输入
  inputChange(e){
    var that = this;
    var name = e.currentTarget.dataset.name;
    var val = e.detail.value;
    that.setData({
      [name]: val
    })
  },

  //切换登录
  checkLogin(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      type,

      account: '',//账号
      password: '',//密码

      phone: '',//手机号码
      simcode: '',//验证码
      getOnecode: '获取验证码',//按钮内容
      last_time: '',//剩余时间
      is_show: true,//是否显示倒计时
    })
    reget = '获取验证码';
    clearTimeout(timer);//移除定时器
    isTimer = false;//timer是否开始
  },

  //账号登录
  accountLogin(){
    var that = this;
    var openid = wx.getStorageSync('openid');
    if (!that.data.account){
      app.showToast('请输入账号');
      return
    }
    if (!that.data.password) {
      app.showToast('请输入密码');
      return
    }
    app.ajax({
      url: "User/User/account_login",
      data: {
        account: that.data.account,
        password: that.data.password,
        openid:openid
      },
      success: function (res) {
        wx.setStorageSync('token', res.data.data.token);
        wx.setStorageSync('user_id', res.data.data.user_id);
        app.showToast(res.data.message, "none", 1000, function () { });
        if (res.data.code == 1000) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
          return false;
        }
      }
    })
  },


  //获取用户手机号码
  getPhoneNumber: function(e) {
    var iv = e.detail.iv,
      encryptedData = e.detail.encryptedData,
      that = this;
    console.log(e)
    if (e.detail.errMsg == 'getPhoneNumber:fail:cancel to confirm login' ||
      e.detail.errMsg == 'getPhoneNumber:fail user deny' || e.detail.rawData == 'undefined') {
      app.showToast('拒绝授权,将无法使用一键登录功能', "none", 2000, function() {});
      return false;
    } else if (e.detail.errMsg == "getPhoneNumber:fail 用户未绑定手机，请先在微信客户端进行绑定后重试") {
      return false;
    } else if (e.detail.errMsg == "getPhoneNumber:fail:cancel to verify sms") {
      return false;
    } else {
      that.getAjaxPhone(iv, encryptedData); //后台解析手机信息
    }
  },

  //后台解析手机号码
  getAjaxPhone(iv, encryptedData) {
    var that = this;
    var openid = wx.getStorageSync('openid');
    app.ajax({
      url: "User/Wx/aesPhoneDecrypt",
      data: {
        iv: iv,
        data: encryptedData,
        openid: openid
      },
      success: function(res) {
        if (res.data.code == 1000) {
          wx.setStorageSync('phone', res.data.data.phoneNumber);
          app.bindGetnewToken(2, '', 1); //重新调取token

        } else {
          app.showToast(res.data.message, "none", 2000, function() {});
          return false;
        }
      }
    })
  },

  //获取验证码
  getCode() {
    var that = this;
    console.log('isTimer:' + isTimer);
    if (isTimer) {
      console.log('timer已经开始');
      return;
    }
    var phone_num = that.data.phone;
    var numreg = /^[1][3-9][\d]{9}$/;
    if (phone_num == '') {
      app.showToast("手机号码不能为空", "none", 2000, function () { });
      return false;
    }
    if (numreg.test(phone_num) == false) {
      app.showToast("请输入正确手机的号码", "none", 2000, function () { });
      return false;
    }
    isTimer = true;
    app.ajax({
      url: "Common/Common/sendSms",
      data: { tel: phone_num },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            is_show: (!that.data.is_show)  //false
          })
          countdown = 120;
          reget = '获取验证码';
          settime(that); //刷新倒计时 
          that.setData({
            getOnecode: reget
          })
          app.showToast(res.data.message, "none", 4000, function () { });
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }
    })
  },
  //短信登录
  simLogin() {
    var that = this;
    var phone_num = that.data.phone;
    var simcode = that.data.simcode;
    if (phone_num == '') {
      app.showToast("手机号码不能为空", "none", 2000, function () { });
      return false;
    } else if (simcode == '') {
      app.showToast("验证码不能为空", "none", 2000, function () { });
      return false;
    }
    wx.setStorageSync('phone', phone_num);
    app.bindGetnewToken(1, simcode, 2);
  },

  //忘记密码
  forgetPassword(){
    wx.navigateTo({
      url: '../password/password?type=1',
    })
  },
  toArticle(e){
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/article/article?type='+type,
    })
  }
})